package com.zu.commu.insertDataServletXml;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.zu.commu.context.ServerContext;
import com.zu.commu.insertDataServletXml.localOperate.SaveLocal;
import com.zu.commu.interf.Data;
import com.zu.commu.interf.saveData.SaveData;
import com.zu.commu.util.DateTime;
import org.apache.log4j.Logger;

public class InsertData implements SaveData {
	private static boolean onceCannotConnectServlet = true;
	private static String year;
	private static String month;
	private static String date;
	private static String hour;
	private static String minute;
	private static String second;
	private static String receiveUrl = ServerContext.toCenterURL;

	private static Logger log = Logger.getLogger(InsertData.class.getName());

	public void insertData(Data data) {
		
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("data", Data.class);
		String xml = xstream.toXML(data);

		boolean flag = new PostDataRemote().post(xml, receiveUrl);

		if (!flag) {
			log.debug("数据发送业务端失败，改为存储本地xml");
			year = DateTime.getYear();
			month = DateTime.getMonth();
			date = DateTime.getDate();
			hour = DateTime.getHour();
			minute = DateTime.getMinute();
			second = DateTime.getSecond();
			//SaveRemote.instance().setCannotRemoteSave();
			SaveLocal.instance().setXmlData(xml);
		}
	}
}
